﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using Fly.Common.Linq;
using Telerik.Web.UI;

namespace RN.ERP.Web.Person
{
    public partial class PersonEmpImportList : System.Web.UI.Page
    {
        #region  公共属性
        /// <summary>
        /// 人员类型
        /// </summary>
        private int classId;

        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Initial();
            }
        }
        /// <summary>
        /// 参数及设置按钮显示
        /// </summary>
        private void Initial()
        {
            ////人员类型
            if (Request.QueryString["classId"] != null)
            {
                int.TryParse(Request.QueryString["classId"], out classId);
            }
            if (Request.QueryString["corpGuid"] != null)
            {
                hdCorpGuid.Value = Request.QueryString["corpGuid"];
            }
            if (Request.QueryString["areaCode"] != null)
            {
                hdAreaCode.Value = Request.QueryString["areaCode"];
            }
            if (Request.QueryString["roleType"] != null)
            {
                hdRoleType.Value = Request.QueryString["roleType"];
            }
            hdClassId.Value = classId.ToString();
           
        }

        #region 取得数据源
        /// <summary>
        /// 取得人员信息查询参数列表
        /// </summary>
        /// <returns></returns>
        private string GetQueryConditions()
        {

            List<ModelService.QueryPersonInfoCondition> query = new List<ModelService.QueryPersonInfoCondition>();
            ModelService.QueryPersonInfoCondition queryCondition = GetQueryCondition();
            query.Add(queryCondition);
            return JsonConvert.SerializeObject(query);
        }
        /// <summary>
        /// 取得人员参数项
        /// </summary>
        /// <param name="flowCode"></param>
        /// <returns></returns>
        private ModelService.QueryPersonInfoCondition GetQueryCondition()
        {
            ///设置查询参数
            ModelService.QueryPersonInfoCondition queryCondition = new ModelService.QueryPersonInfoCondition();
            queryCondition.TypeFlag = new ParameterValue<string> { V = "-1", O = OptEnum.Like };
            int.TryParse(hdClassId.Value, out classId);
            if (classId > 0)
            {
                queryCondition.ClassId = new ParameterValue<int?> { V = classId, O = OptEnum.Equal };
            }
            if (!string.IsNullOrEmpty(txtEmpName.Text.Trim()))
            {
                queryCondition.Name = new ParameterValue<string> { V = txtEmpName.Text.Trim(), O = OptEnum.Like };
            }
            if (!string.IsNullOrEmpty(txtEmpIdCard.Text.Trim()))
            {
                queryCondition.IDCard = new ParameterValue<string> { V = txtEmpIdCard.Text.Trim(), O = OptEnum.Equal };
            }
            ///用户端登陆
            if (hdRoleType.Value == "User")
            {
                Guid userGuid = Guid.Empty;
                Guid.TryParse(hdCorpGuid.Value, out userGuid);
                queryCondition.CorpGuid = new ParameterValue<Guid> { V = userGuid, O = OptEnum.Equal };
            }
            return queryCondition;
        }
        /// <summary>
        /// 取得人员基本信息
        /// </summary>
        /// <param name="empGuid"></param>
        /// <param name="areaCode"></param>
        private List<ModelService.tb_Emp_Manager> GetEmpManagersList(string areaCode, int currentPageIndex, int pageSize)
        {
            try
            {
                using (PersonService.PersonServiceClient client = new PersonService.PersonServiceClient())
                {
                    string queryStr = GetQueryConditions();
                    rgPersonList.VirtualItemCount = client.GetEmpImportCount(queryStr, areaCode);
                    string result = client.GetEmpImportList(currentPageIndex + 1, pageSize, queryStr, areaCode);
                    return JsonConvert.DeserializeObject<List<ModelService.tb_Emp_Manager>>(result).ToList();
                }
            }
            catch
            {
                return null;
            }
        }
        /// <summary>
        /// 绑定人员数据列表
        /// </summary>
        private void rgPersonDataBind()
        {
            int currentPageIndex = rgPersonList.CurrentPageIndex;
            int pageSize = rgPersonList.MasterTableView.PageSize;
            rgPersonList.DataSource = GetEmpManagersList(hdAreaCode.Value, currentPageIndex, pageSize);

        }
        /// <summary>
        /// 绑定查询列表
        /// </summary>
        private void PersonDataBind()
        {
            rgPersonList.CurrentPageIndex = 0;
            rgPersonDataBind();
            rgPersonList.DataBind();
        }
        #endregion

        #region 事件处理

        /// <summary>
        /// 绑定人员信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void rgPersonList_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            rgPersonDataBind();

        }
      
        /// <summary>
        /// 条件查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbtnEmpSreach_Click(object sender, EventArgs e)
        {
            PersonDataBind();
        }
        /// <summary>
        /// 客户端grid刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            if (e.Argument == "Rebind")
            {
                rgPersonList.MasterTableView.SortExpressions.Clear();
                rgPersonList.MasterTableView.GroupByExpressions.Clear();
                rgPersonList.Rebind();
            }

        }

        #endregion
    }
}